#line 1 "C:/Documents and Settings/Administrator/My Documents/TFC/trunk/datalogger.c"
#line 1 "c:/documents and settings/administrator/my documents/tfc/trunk/config.h"





sbit LCD_RS at RA4_bit;
sbit LCD_EN at RC0_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;


sbit LCD_RS_Direction at TRISA4_bit;
sbit LCD_EN_Direction at TRISC0_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;


void InitMain() {

 TRISA = 0x0F;
 PORTA = 0x00;

 PORTB = 0x00;
 TRISB = 0xF0;

 TRISC = 0x80;
 PORTC = 0x00;



}
#line 1 "c:/documents and settings/administrator/my documents/tfc/trunk/custom_lib.h"

void write_lcd(long input, int lin, int col)
{
int ch;
long value;

if (input >= 0) {
 value = input;
 }
 else {
 value = ~input + 1;
 }

ch = value / 1000;

if ( input >= 0 )
 LCD_Chr( lin, col, '+' );
 else LCD_Chr( lin, col, '-' );

LCD_Chr_CP( 48+ch );
LCD_Chr_CP('.');

ch = ( value / 100 ) % 10;
LCD_Chr_CP( 48 + ch );

ch = ( value / 10 ) % 10;
LCD_Chr_CP( 48 + ch );

ch = value % 10;
LCD_Chr_CP( 48 + ch );
}


void clean_marks() {
 LCD_Chr( 1, 8, ' ' );
 LCD_Chr( 2, 8, ' ' );
}

int testa_uart() {
 UART1_Write_Text("ok");
 UART1_Write(10);
 UART1_Write(13);
return 0;
}

unsigned long media(unsigned char channel, unsigned char qty)
{
 unsigned int vlMedia = 0;
 unsigned char i;

 for(i=0; i<qty; i++) {
 vlMedia += ADC_Read(channel);
 }
 return vlMedia/qty;
}
#line 1 "c:/documents and settings/administrator/my documents/tfc/trunk/spi.h"
#line 14 "c:/documents and settings/administrator/my documents/tfc/trunk/spi.h"
void spiInit();
char spiReadWait();
int spiRead(char *pSpiData);
void spiWrite(char spiData);
#line 1 "c:/documents and settings/administrator/my documents/tfc/trunk/monitor.h"
#line 1 "c:/documents and settings/administrator/my documents/tfc/trunk/spi.h"
#line 69 "c:/documents and settings/administrator/my documents/tfc/trunk/monitor.h"
enum vResponse {

 Resp_Prompt_OK,
 Resp_Prompt_ND,
 Resp_Prompt_UE,
 Resp_Prompt_LE,
 Resp_Prompt_CF,
 Resp_Prompt_BC,
 Resp_Prompt_DF,
 Resp_Prompt_FI,
 Resp_Prompt_RO,
 Resp_Prompt_FO,
 Resp_Prompt_NE,
 Resp_Prompt_FN,
 Resp_Prompt_End,

 Resp_Message_NU,
 Resp_Message_DD1,
 Resp_Message_DD2,
 Resp_Message_DR1,
 Resp_Message_DR2,
 Resp_Message_Splash,
 Resp_None = 0xff
};

void monSendByte(char monData);
void monCmdSend(char monCmd);
void monCmdSendByteParam(char monCmd, unsigned char monParam);
void monCmdSendParam(char monCmd, unsigned char monCount, unsigned char *pmonParam);
enum vResponse monResponse();
enum vResponse monPrompt();
#line 12 "C:/Documents and Settings/Administrator/My Documents/TFC/trunk/datalogger.c"
long sens0 = 0;
long sens1 = 0;
long sens2 = 0;
long sens3 = 0;

long offset_sens0 = 0;
long offset_sens1 = 0;
long offset_sens2 = 0;
long offset_sens3 = 0;


const float adc_mult = 4.88758546;
char *result = "ok";
long wrBuffer[4];


unsigned short st_sel = 0;
unsigned short st_ent = 0;
unsigned short st_rec = 0;
unsigned short st_clear = 0;


void interrupt(void){
 if (INTCON.RBIF == 1) {

 if (Button(&PORTB, 5, 100, 0)){

 st_sel = (st_sel > 3) ? 1 : ++st_sel;
 }

 if (Button(&PORTB, 6, 100, 0)){

 st_ent = 1;
 }

 if (Button(&PORTB, 7, 100, 0)){

 st_rec = 1;
 }

 if (Button(&PORTB, 4, 100, 0)) {

 st_clear = 1;
 }

 INTCON.RBIF = 0;
 }
}

void escreve_leitura_adc()
{
 write_lcd((sens0 - offset_sens0), 1, 1);
 if (offset_sens0 != 0) LCD_Chr_CP('*');

 write_lcd((sens1 - offset_sens1), 1, 10);
 if (offset_sens1 != 0) LCD_Chr_CP('*');

 write_lcd((sens2 - offset_sens2), 2, 1);
 if (offset_sens2 != 0) LCD_Chr_CP('*');

 write_lcd((sens3 - offset_sens3), 2, 10);
 if (offset_sens3 != 0) LCD_Chr_CP('*');
}

void main() {
 InitMain();
 Lcd_init();
 Lcd_Cmd(_LCD_CURSOR_OFF);

 OPTION_REG = 0xFF;
 INTCON.RBIF = 0;
 INTCON.RBIE = 1;
 INTCON.GIE = 1;

 UART1_Init(9600);
 Delay_ms(100);

 spiInit();
 Delay_ms(100);

 PWM2_Init(1000);
 PWM2_Start();
 PWM2_Set_Duty(127);





while(1) {


 sens0 = ( (long)media(0,  8 ) * adc_mult );
 sens1 = ( (long)media(1,  8 ) * adc_mult );
 sens2 = ( (long)media(2,  8 ) * adc_mult );
 sens3 = ( (long)media(3,  8 ) * adc_mult );

 escreve_leitura_adc();

 if(st_sel > 0){
 switch (st_sel) {
 case 1: clean_marks(); LCD_Chr( 1, 8, '<' ); break;
 case 2: clean_marks(); LCD_Chr( 1, 8, '>' ); break;
 case 3: clean_marks(); LCD_Chr( 2, 8, '<' ); break;
 case 4: clean_marks(); LCD_Chr( 2, 8, '>' ); break;
 }
 }

 if (st_ent == 1) {
 switch(st_sel) {
 case 1: offset_sens0 = sens0; break;
 case 2: offset_sens1 = sens1; break;
 case 3: offset_sens2 = sens2; break;
 case 4: offset_sens3 = sens3; break;
 }
 st_ent = 0;
 }


 if (st_rec) {


 result = "test";
 monCmdSendParam( 0x06 , 4, result);
 UART1_Write_Text("resposta = ");
 UART1_Write_Text(result);
 UART1_Write(10);
 UART1_Write(13);
 monCmdSendParam( 0x02 , 4, "test");

 monCmdSendParam( 0x09 , 8, "log1.txt");
 wrBuffer[0] = sens0 - offset_sens0;
 wrBuffer[1] = sens1 - offset_sens1;
 wrBuffer[2] = sens2 - offset_sens2;
 wrBuffer[3] = sens3 - offset_sens3;
 monCmdSendParam( 0x08 , 4, wrBuffer);
 monCmdSendParam( 0x08 , 4, wrBuffer);
 monCmdSendParam( 0x0a , 8, "log1.txt");


 st_rec = 0;
 }

 if(st_clear == 1) {
 LCD_Out( 1, 7, "  " );
 LCD_Out( 2, 7, "  " );
 LCD_Chr( 1, 16, ' ' );
 LCD_Chr( 2, 16, ' ' );
 st_sel = 0;
 offset_sens0 = 0;
 offset_sens1 = 0;
 offset_sens2 = 0;
 offset_sens3 = 0;
 st_clear = 0;
 }

 Delay_ms(100);
 }
}
